package find.prj.lib;


public class OneList<T> extends ListBase<T> {

	protected ListNode<T> FirstNode;
	
	public OneList() {
		this.FirstNode = null;
		this.Size = 0;
	}
	
	public OneList(T value) {
		this();
		this.add(value);		
	}
	
	public OneList(T[] valueArray) {
		this();
		for (T value : valueArray)
			this.add(value);
	}
	
	@Override
	public void clear() {
		this.FirstNode = null;
		this.Size = 0;
	}

	@Override
	protected ListNode<T> getListNode(int index) {
		if ((index < this.Size) && (index >= 0)) {
			ListNode<T> node = this.FirstNode;
			for(int k=0; k <= index ; k++) {
				if (k == index)
					return node;
				node = node.NextNode;
			}
		}
		else
			throw new IndexOutOfBoundsException();
		return null;		
	}

	@Override
	public boolean add(T value) {
		if (this.isEmpty())
			this.FirstNode = new ListNode<T>(value);
		else
			this.getListNode(this.Size-1).NextNode = new ListNode<T>(value);
		this.Size++;
		return true;
	}

	@Override
	public void removeIndex(int index) {
		if ( (index < this.Size) && (index >= 0) && (this.Size > 0) ) {
			if (Size == 1)
				this.FirstNode = null;
			else
				if (index == 0)
					this.FirstNode = this.FirstNode.NextNode;
				else
					this.getListNode(index-1).NextNode = this.getListNode(index).NextNode;
			this.Size--;
		}
		else
			throw new IndexOutOfBoundsException(); 
	}

}
